package cn.siat.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.siat.common.core.utils.StringUtils;
import cn.siat.common.core.web.controller.BaseController;
import cn.siat.common.core.web.domain.AjaxResult;
import cn.siat.common.core.web.page.TableDataInfo;
import cn.siat.common.log.annotation.Log;
import cn.siat.common.log.enums.BusinessType;
import cn.siat.common.security.utils.SecurityUtils;
import cn.siat.system.api.domain.SysReleateAccount;
import cn.siat.system.service.ISysReleateAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 关联账户
 * @Author: Administrator
 * @Date: 2022-10-23
 * @Version: V1.0
 */
@Api(tags = "关联账户")
@RestController
@RequestMapping("/releateaccount")
@RequiredArgsConstructor
@Slf4j
public class SysReleateAccountController extends BaseController {
    @Autowired
    private ISysReleateAccountService sysReleateAccountService;

    /**
     * 全部数据查询
     *
     * @param sysReleateAccount
     * @return
     */
    @ApiOperation(value = "关联账户-全部数据查询", notes = "关联账户-全部数据查询")
    @GetMapping(value = "/listall")
    public AjaxResult queryAllList(SysReleateAccount sysReleateAccount) {
        QueryWrapper<SysReleateAccount> queryWrapper = new QueryWrapper<SysReleateAccount>();
        queryWrapper.orderByDesc("create_time");
        List<SysReleateAccount> list = sysReleateAccountService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 分页列表查询
     *
     * @param sysReleateAccount
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "关联账户-分页列表查询", notes = "关联账户-分页列表查询")
    @GetMapping(value = "/list")
    public TableDataInfo queryPageList(SysReleateAccount sysReleateAccount,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
        QueryWrapper<SysReleateAccount> queryWrapper = new QueryWrapper<SysReleateAccount>();
        Page<SysReleateAccount> page = new Page<SysReleateAccount>(pageNo, pageSize);
        IPage<SysReleateAccount> pageList = sysReleateAccountService.page(page, queryWrapper);
        List<SysReleateAccount> list = pageList.getRecords();
        return getDataTableCustom(list, pageList.getTotal());
    }

    /**
     * 添加
     *
     * @param sysReleateAccount
     * @return
     */
    @Log(title = "关联账户-添加", businessType = BusinessType.INSERT)
    @ApiOperation(value = "关联账户-添加", notes = "关联账户-添加")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody SysReleateAccount sysReleateAccount) {
        sysReleateAccountService.save(sysReleateAccount);
        return AjaxResult.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysReleateAccount
     * @return
     */
    @Log(title = "关联账户-编辑", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "关联账户-编辑", notes = "关联账户-编辑")
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody SysReleateAccount sysReleateAccount) {
        sysReleateAccountService.updateById(sysReleateAccount);
        return AjaxResult.success("编辑成功！");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Log(title = "关联账户-通过id删除", businessType = BusinessType.DELETE)
    @ApiOperation(value = "关联账户-通过id删除", notes = "关联账户-通过id删除")
    @DeleteMapping(value = "/delete")
    public AjaxResult delete(@RequestParam(name = "id", required = true) String id) {
        sysReleateAccountService.removeById(id);
        return AjaxResult.success("删除成功！");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Log(title = "关联账户-批量删除", businessType = BusinessType.DELETE)
    @ApiOperation(value = "关联账户-批量删除", notes = "关联账户-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public AjaxResult deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysReleateAccountService.removeByIds(Arrays.asList(ids.split(",")));
        return AjaxResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "关联账户-通过id查询", notes = "关联账户-通过id查询")
    @GetMapping(value = "/queryById")
    public AjaxResult queryById(@RequestParam(name = "id", required = true) String id) {
        SysReleateAccount sysReleateAccount = sysReleateAccountService.getById(id);
        if (sysReleateAccount == null) {
            return AjaxResult.error("未找到对应数据");
        }
        return AjaxResult.success(sysReleateAccount);
    }

    /**
     * 绑定账户
     *
     * @param sysReleateAccount
     * @return
     */
    @Log(title = "关联账户-绑定账户", businessType = BusinessType.INSERT)
    @ApiOperation(value = "关联账户-绑定账户", notes = "关联账户-绑定账户")
    @PostMapping(value = "/bind")
    public AjaxResult bind(@RequestBody SysReleateAccount sysReleateAccount) {
        if (StringUtils.isEmpty(sysReleateAccount.getId())) {
            return AjaxResult.error("系统ID不能为空！");
        }

        SysReleateAccount data = sysReleateAccountService.getById(sysReleateAccount.getId());
        if (data == null) {
            return AjaxResult.error("该系统不存在");
        }

        if (StringUtils.isEmpty(sysReleateAccount.getBindAccount())) {
            return AjaxResult.error("绑定用户名不能为空！");
        }

        if (StringUtils.isEmpty(sysReleateAccount.getBindPassword())) {
            return AjaxResult.error("绑定密码不能为空！");
        }

        sysReleateAccount.setBindStatus(1);
        sysReleateAccount.setUpdateTime(new Date());
        sysReleateAccount.setBindTime(new Date());
        sysReleateAccount.setUpdateBy(SecurityUtils.getUsername());
        sysReleateAccountService.updateById(sysReleateAccount);
        return AjaxResult.success("绑定成功！");
    }

    /**
     * 解除绑定账户
     *
     * @param sysReleateAccount
     * @return
     */
    @Log(title = "关联账户-解除绑定账户", businessType = BusinessType.INSERT)
    @ApiOperation(value = "关联账户-解除绑定账户", notes = "关联账户-解除绑定账户")
    @PostMapping(value = "/unbind")
    public AjaxResult unbind(@RequestBody SysReleateAccount sysReleateAccount) {
        if (StringUtils.isEmpty(sysReleateAccount.getId())) {
            return AjaxResult.error("系统ID不能为空！");
        }

        SysReleateAccount data = sysReleateAccountService.getById(sysReleateAccount.getId());
        if (data == null) {
            return AjaxResult.error("该系统不存在");
        }

        data.setUpdateTime(new Date());
        data.setBindTime(null);
        data.setBindAccount(null);
        data.setBindPassword(null);
        sysReleateAccountService.updateById(data);
        return AjaxResult.success("解绑成功！");
    }
}
